package com.csust.automaticteach.app.common;

import com.csust.automaticteach.app.advisor.MyLoggerAdvisor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.util.List;

import static com.csust.automaticteach.constant.AppPromptConstant.EVALUATE_SYSTEM_ROLE;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @ClassNameExerciseGenerateAndEvaluateApp
 * @Description 练习生成与评测助手
 * @Author钰玟
 * @Date2025/6/20 上午9:12
 * @Version 1.0
 **/
@Component
@Slf4j
public class ExerciseEvaluateApp {
    private ChatClient chatClient;


    public ExerciseEvaluateApp(ChatModel dashscopeChatModel) {
        //基于内存的会话记忆存储
        ChatMemory chatMemory = new InMemoryChatMemory();
        this.chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem(EVALUATE_SYSTEM_ROLE)//系统预设
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory),//会话记忆存储
                        new MyLoggerAdvisor())//日志记录
                .build();
    }

    public record judgeResponse(Long id,boolean isCorrect, String improvement){}
    /**
     * 支持多轮对话记忆的 聊天
     *
     * @param judgeExercisesRequestForAI 用户的问题
     * @param chatId                             会话id
     * @return List<judgeResponse>
     */
    public List<judgeResponse> doEvaluate(String judgeExercisesRequestForAI, String chatId) {
// 1. 整体转义，让 ST4 不再报错
        String escaped = StringEscapeUtils.escapeJson(judgeExercisesRequestForAI);
        List<judgeResponse> responseList = chatClient.prompt()
                .user(escaped)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(
                        CHAT_MEMORY_RETRIEVE_SIZE_KEY, 5))
                .call()
                .entity(new ParameterizedTypeReference<List<judgeResponse>>() {});
        log.info("evaluate result :{}", responseList);
        return responseList;
    }
    /**
     * 支持多轮对话记忆的 聊天
     *
     * @param judgeExercisesRequestForAI 用户的问题
     * @param chatId                             会话id
     * @return Flux<String>
     */
    public Flux<String> doEvaluateByStream(String judgeExercisesRequestForAI, String chatId) {
        // 1. 整体转义，让 ST4 不再报错
        String escaped = StringEscapeUtils.escapeJson(judgeExercisesRequestForAI);
        Flux<String> content = chatClient.prompt()
                .user(judgeExercisesRequestForAI)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(
                        CHAT_MEMORY_RETRIEVE_SIZE_KEY, 5))
                .stream()
                .content();
        log.info("evaluate result :{}", content);
        return content;
    }

}
